package com.jinxin.platform.cdsmsmgr.service.impl;

import com.jinxin.platform.cdsmsmgr.mapper.SmsBusinessInfoMapper;
import com.jinxin.platform.cdsmsmgr.pojo.domains.SmsBusinessInfo;
import com.jinxin.platform.cdsmsmgr.pojo.domains.SysUserVO;
import com.jinxin.platform.cdsmsmgr.pojo.entity.CommonCodeEntity;
import com.jinxin.platform.cdsmsmgr.pojo.entity.Constant;
import com.jinxin.platform.cdsmsmgr.pojo.entity.ResultEntity;
import com.jinxin.platform.cdsmsmgr.rpc.SysUserRpc;
import com.jinxin.platform.cdsmsmgr.service.ISmsBusinessService;
import com.jinxin.platform.cdsmsmgr.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2020-06-29
 * @Author: Lingnan
 **/
@Service
public class SmsBusinessServiceImpl implements ISmsBusinessService {

    @Autowired
    private SmsBusinessInfoMapper smsBusinessMapper;
    @Autowired
    private SysUserRpc sysUserRpc;

    @Override
    public List<SmsBusinessInfo> listPage(HashMap param) {
        return smsBusinessMapper.listPage(param);
    }

    @Override
    public int listPageCount(HashMap param) {
        return smsBusinessMapper.listPageCount(param);
    }

    @Override
    public ResultEntity saveOrUpdate(SmsBusinessInfo businessInfo) {
        SysUserVO currentUser = sysUserRpc.getCurrentUser();
        if(currentUser == null) {
            return new ResultEntity(CommonCodeEntity.NO_LOGIN,"未登录用户","");
        }
        int count;
        if(!StringUtils.isEmpty(businessInfo.getId())){
            count = smsBusinessMapper.updateByKey(businessInfo);
        }else {
            // 查看相同业务名称的是否已经存在
            String name = businessInfo.getName();
            SmsBusinessInfo smsBusinessInfo = getBusinessInfo(name,businessInfo.getType(),businessInfo.getAccountType());
            if(smsBusinessInfo != null){
                return new ResultEntity(CommonCodeEntity.ALREADY_EXIST,"相同业务类型已存在","");
            }
            businessInfo.setCreateTime(LocalDateTime.now());
            businessInfo.setId(Utils.getMillPrimaryKey());
            businessInfo.setApplyUser(currentUser.getUserAccount());
            businessInfo.setIsSetSign(Constant.SMS_BUSINESS_UNSETTED);
            businessInfo.setIsSetTemplate(Constant.SMS_BUSINESS_UNSETTED);
            count = smsBusinessMapper.insert(businessInfo);
        }
        if(count > 0){
            return new ResultEntity(CommonCodeEntity.SUC_CODE,"suc",businessInfo);
        }else {
            return new ResultEntity(CommonCodeEntity.FAIL,"保存失败","");
        }
    }

    private SmsBusinessInfo getBusinessInfo(String name,Integer type,Integer accountType) {
        SysUserVO currentUser = sysUserRpc.getCurrentUser();
        if(currentUser == null || StringUtils.isEmpty(name)|| StringUtils.isEmpty(type)) {
            return null;
        }
        String userAccount = currentUser.getUserAccount();
        if(!StringUtils.isEmpty(userAccount)){
            HashMap param = new HashMap();
            param.put("completeName",name);
            param.put("type",type);
            param.put("applyUser",userAccount);
            param.put("accountType", accountType);
            return smsBusinessMapper.selectOne(param);
        }
        return null;
    }

    @Override
    public SmsBusinessInfo getOne(HashMap param) {
        return smsBusinessMapper.selectOne(param);
    }

    @Override
    public ResultEntity deleteById(String id) {
        if(StringUtils.isEmpty(id)){
            return new ResultEntity(CommonCodeEntity.PARAM_ERR,"id为空","");
        }
        int count = smsBusinessMapper.deleteByKey(id);
        if(count > 0){
            return new ResultEntity(CommonCodeEntity.SUC_CODE,"suc","");
        }
        return new ResultEntity(CommonCodeEntity.FAIL,"删除失败","");
    }
}
